package com.sample.navigation.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import com.sample.navigation.R;
import androidx.appcompat.widget.AppCompatTextView;

public class CircleTextView extends AppCompatTextView {
    private int backgroundColor_start = 0;
    private int backgroundColor_end = 0;
    private int background_normal = 0;
    private int clrHighLight = 0;
    private int clrText = 0;
    private Paint paint;
    private boolean highHight = false;
    private RectF rect;
    public CircleTextView(Context context) {
        this(context, null);
    }

    public CircleTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleTextView);
        clrText = ta.getColor(R.styleable.CircleTextView_circle_text_text_color, Color.BLACK);
        clrHighLight = ta.getColor(R.styleable.CircleTextView_circle_text_high_light_color, Color.WHITE);
        background_normal = ta.getColor(R.styleable.CircleTextView_circle_text_background_normal, Color.TRANSPARENT);
        backgroundColor_start = ta.getColor(R.styleable.CircleTextView_circle_text_background_start, Color.TRANSPARENT);
        backgroundColor_end = ta.getColor(R.styleable.CircleTextView_circle_text_background_end, Color.TRANSPARENT);
        ta.recycle();

        rect = new RectF();
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    private void drawCenter(String text, RectF rt, Canvas canvas){
        if(!rt.isEmpty()) {
            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
            float offsetY = rt.height();
            float baseline = rt.top + offsetY * 0.5f + distance;
            canvas.drawText(text, rt.centerX(), baseline, paint);
        }
    }

    public void setHighLight(boolean highHight){
        this.highHight = highHight;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        rect.left = getPaddingLeft();
        rect.top = getPaddingTop();
        rect.right = w - getPaddingRight();
        rect.bottom = h - getPaddingBottom();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(highHight){
            LinearGradient linearGradient = new LinearGradient(
                    rect.left, rect.top,
                    rect.right,rect.bottom,
                    backgroundColor_start,
                    backgroundColor_end,
                    Shader.TileMode.CLAMP);
            paint.setShader(linearGradient);
            canvas.drawCircle(rect.centerX(), rect.centerY(),
                    rect.width() > rect.height() ? rect.height()/2 : rect.width()/2, paint);
            paint.setColor(clrHighLight);
        }else{
            LinearGradient linearGradient = new LinearGradient(
                    rect.left, rect.top,
                    rect.right,rect.bottom,
                    background_normal,
                    background_normal,
                    Shader.TileMode.CLAMP);
            paint.setShader(linearGradient);
            canvas.drawCircle(rect.centerX(), rect.centerY(),
                    rect.width() > rect.height() ? rect.height()/2 : rect.width()/2, paint);
            paint.setColor(clrText);
        }
        paint.setShader(null);
        paint.setStrokeWidth(1);
        paint.setTextSize(getTextSize());
        paint.setTextAlign(Paint.Align.CENTER);
        drawCenter(getText().toString(), rect, canvas);
    }
}
